<?php 
/*
 module:		另一种弹窗控制器
 create_time:	2022-08-21 10:24:22
 author:		
 contact:		
*/

namespace app\Controller\admin;
use function Hyperf\ViewEngine\view;
use App\Exception\ValidateException;
use App\Model\admin\MemModel;
use App\Controller\admin\CommonController;
use Hyperf\DbConnection\Db;

class MemController extends CommonController {


	/*
 	* @Description  数据列表
 	*/
	function index(){
		if($this->request->isMethod('get')){
			return view('admin/mem/index');
		}else{
			$limit  = $this->request->input('limit', 20);
			$page = $this->request->input('page', 1);

			$order  = $this->request->input('order');	//排序方式
			$sort  = $this->request->input('sort');		//排序字段

			$where = [];
			$where['membe_id'] = $this->request->input('membe_id');
			$where['username'] = $this->request->input('username');
			$where['sex'] = $this->request->input('sex');
			$where['mobile'] = $this->request->input('mobile');
			$where['email'] = $this->request->input('email');
			$where['status'] = $this->request->input('status');
			$where['ssq'] = ['like',implode('-',$this->request->input('ssq',[]))];

			$create_time = $this->request->input('create_time');
			if(isset($create_time[0]) && isset($create_time[1])){
				$where['create_time'] = ['between',[strtotime($create_time[0]),strtotime($create_time[1])]];
			}

			$sort = !empty($sort) ? $sort : 'membe_id';
			$order = !empty($order) ? $order : 'desc';

			$field = 'membe_id,username,sex,pic,mobile,email,amount,status,ssq,create_time';

			$query = MemModel::select(explode(",",$field));

			$res = formatWhere($query,$where)->orderBy($sort,$order)->paginate($limit)->toArray();

			$data['status'] = 200;
			$data['data'] = $res;
			return json($data);
		}
	}


	/*
 	* @Description  添加
 	*/
	public function add(){
		$postField = 'username,sex,pic,mobile,email,password,amount,status,ssq,create_time';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\MemValidate;
		if(!$validate->scene('add')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['password'] = md5($data['password'].config('my.password_secrect'));
		$data['ssq'] = implode('-',$data['ssq']);
		$data['create_time'] = time();

		try{
			$res = MemModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/*
 	* @Description  修改
 	*/
	public function update(){
		$postField = 'membe_id,username,sex,pic,mobile,email,amount,status,ssq,create_time';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\MemValidate;
		if(!$validate->scene('update')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['ssq'] = implode('-',$data['ssq']);
		$data['create_time'] = strtotime($data['create_time']);

		try{
			MemModel::where('membe_id',$data['membe_id'])->update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	function getUpdateInfo(){
		$id =  $this->request->input('membe_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'membe_id,username,sex,pic,mobile,email,amount,status,ssq,create_time';
		$res = MemModel::select(explode(",",$field))->find($id);
		$res['ssq'] = explode('-',$res['ssq']);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  删除
 	*/
	function delete(){
		$idx =  $this->request->input('membe_id');
		if(!$idx) throw new ValidateException ('参数错误');
		MemModel::whereIn('membe_id',explode(',',$idx))->delete();
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/*
 	* @Description  查看详情
 	*/
	function detail(){
		$id =  $this->request->input('membe_id');
		if(!$id) throw new ValidateException ('参数错误');

		$field = 'membe_id,username,sex,pic,mobile,email,amount,status,ssq,create_time';

		$res = MemModel::select(explode(",",$field))->find($id);
		return json(['status'=>200,'data'=>$res]);
	}






}



